<?php
	header('content-type:text/html;charset=utf-8');
	$cho=$_GET["cho"];//工单号
	$lot=$_GET["lot"];//工单号
	$dbName = "sqlsrv:Server=172.31.3.12,1433;Database=UFDATA_001_2017";
	$dbUser = "sa";
	$dbPassword = "TDled!2018";
	$db = new PDO($dbName, $dbUser, $dbPassword);

	$sql = "
	select
		 MoDId
		,InvCode
	from
		mom_orderdetail
	where
		moid=
		(
			select
				MoId
			from
				mom_order
			where
				MoCode ='".$lot."'
		)"
	;

	$res = $db->query($sql);
	$MoDId="";
	$InvCode="";
	$Remark="";
	while ($row = $res->fetch())
	{
		$MoDId=$row['MoDId'];
		$InvCode=$row['InvCode'];
	}

	if($cho == "")
	{
		$sql = "
		select
			 invcode
			,Qty
			,Remark
		from
			mom_moallocate
		where
			MoDId = '".$MoDId."'
		and
			invcode like 'A%'"
		;
	}
	else
	{
		$sql = "
		select
			 invcode
			,Qty
			,Remark
		from
			mom_moallocate
		where
			MoDId = '".$MoDId."'
		and
			invcode = '".$cho."'"
		;
	}

	$res2 = $db->query($sql);
	$sub_invcode="";
	$Qty="";
    $i=0;

	while ($row = $res2->fetch())	{
	  $i+=1;
		if ($i==1)		{//取芯片1料號和數量
			$sub_invcode1= $row['invcode'];//晶片料号
			$Qty=$row['Qty'];//晶片数量
			$Remark=$row['Remark'];//备注
		}

		if ($i==2)
		{//取芯片2料號和數量
			$sub_invcode2= $row['invcode'];
			$Qty+=$row['Qty'];
			$Remark2=$row['Remark'];
		}
	}

	//取芯片1料號明細
	$sql = "
	select
		 cInvName
		,cInvDefine10
		,cInvDefine3
		,cInvDefine2
	from
		Inventory
	where
		cinvcode='".$sub_invcode1."'"
	;

	$res = $db->query($sql);
	$cInvName="";
	$ele="";
	$light="";
	$wave="";

	while ($row = $res->fetch())
	{
	  $cInvName= $row['cInvName'];//芯片名
	  $ele=$row['cInvDefine10'];//电压
	  $light= $row['cInvDefine3'];//亮度
	  $wave=$row['cInvDefine2'];//波长
	}

	if ($i>=2)
	{//取芯片2料號明細
		$sql = "
		select
			cInvName
			,cInvDefine10
			,cInvDefine3
			,cInvDefine2
		from
			Inventory
		where
			cinvcode='".$sub_invcode2."'"
		;

		$res = $db->query($sql);
		$cInvName2="";
		$ele2="";
		$light2="";
		$wave2="";

		while ($row = $res->fetch())
		{
		  $cInvName2= $row['cInvName'];//芯片名
		  $ele2=$row['cInvDefine10'];//电压
		  $light2= $row['cInvDefine3'];//亮度
		  $wave2=$row['cInvDefine2'];//波长
		}
	}

	$res = null;
	$res2 = null;
	$db = null;
	if ($i>=2)
	{//芯片料號個數大於1時，就湊下面這句
		$wo= $InvCode.",".$sub_invcode1.";".$sub_invcode2.",".$Qty.",".$cInvName2.",".$ele.";".$ele2.",".$light.";".$light2.",".$wave.";".$wave2.",".$Remark.";".$Remark2;
	}
	else
	{//否則就原來湊的那樣
		$wo= $InvCode.",".$sub_invcode1.",".$Qty.",".$cInvName.",".$ele.",".$light.",".$wave.",".$Remark;
	}//T2027911A-M6701A,A233000;A2330A0,760.512,晶片/S-30MBMUD;晶片/S-30MBMUD,2.8-3.1;3.1-3.2,155-162;155-162,452.5-455

	print_r($wo);
?>